
public class MySingleLinkedList implements IList {
    static class ListNode{
        public int val;
        public  ListNode next;
        public  ListNode(int val){
            this.val = val;
        }
    }

    //链表的属性  链表的头节点
    public ListNode head;//null

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else {
                ListNode cur = head;
                while (cur.next != null) {
                cur = cur.next;
               }
                cur.next = node;
               }

    }

    @Override//任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        return false;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key  ){
            head = head.next;
            return;
        }
        ListNode cur = findprevkey(key);
        if(cur == null) {
            return;//没有你要删除的数字
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
        private ListNode findprevkey(int key){
            ListNode cur = head;
            while(cur.next != null){
                if(cur.val == key){
                    return cur;
                }else{
                    cur = cur.next;
                }

            }
            return null;
        }



    @Override//删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val ==key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev =cur;
                cur = cur.next;
            }  //除了头节点都删除完成了
            if(head.val == key){
                head = head.next;
            }
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(head != null){
            count++;
            head = head.next;
        }
        return  count;
    }
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val +"");
            cur = cur.next;
        }
        System.out.println();

    }

    @Override
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val +"");
            cur = cur.next;
        }
        System.out.println();

    }

    @Override
    public void clear() {
        /*ListNode cur = head;
        while(head != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }*/
         head = null;
    }



}
